




/*
*************************************************************************
*西瑞克斯研发管理系统
*版 本: 1.0   
*日 期：02/16/2012    
*作 者： 王浩 QQ：873987060   
*************************************************************************   
*/ 
package com.application.yfappointtask.struts.actions;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.manager.commons.config.ConfigFactory;
import com.manager.commons.page.PageBean;
import com.manager.commons.DisplayTagPageBean;
import com.manager.commons.RandomKey;
import com.manager.commons.CommonUtil;
import com.manager.workflow.WorkFlowConstants;
import com.manager.workflow.core.WorkFlowCommon;
import com.application.attachment.entity.Attachment;
import com.application.attachment.service.AttachmentService;
import com.application.attachment.struts.form.AttachmentForm;
import com.application.usertable.entity.Usertable;
import com.application.yfappointtask.entity.YfAppointTask;
import com.application.yfappointtask.struts.form.YfAppointTaskForm;
import com.application.yfappointtask.service.YfAppointTaskService;
import com.application.yfappointtask.commons.Constants;
import com.application.yfexamine.entity.YfExmaine;
import com.application.yfexamine.service.YfExmaineService;
import com.application.yfexamine.struts.form.YfExmaineForm;
import com.application.yfoutsourcecontarct.struts.form.YfOutsourceContractForm;
import com.application.yfproject.service.YfProjectService;
 /**
 * 
 * Description:YfAppointTaskAction<br />
 * 
 * Version:V0.1<br />
 * 
 * Author: 王浩 QQ：873987060    <br />
 * 
 * History: <br />
 * 
 * 02/16/2012 
 * 
 */
public class YfAppointTaskAction extends DispatchAction
{
       
       /**
	 * Service层实例
	 */
	private YfAppointTaskService yfAppointTaskService;
	
	private AttachmentService attachmentService;
	
	private WorkFlowCommon workFlowCommon;
	
	private YfExmaineService yfExmaineService;
	
	private YfProjectService yfProjectService;

	/**
	 * 日志
	 */
	private Logger log = Logger.getLogger(this.getClass());

	/**
	 * 实体对象
	 */
        private YfAppointTask yfAppointTask;


        /**
	 * 采用partialList方式分页程序
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ActionForward getYfAppointTaskPartialList(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("searchYfAppointTask" + "开始");
		YfAppointTaskForm yfAppointTask = (YfAppointTaskForm)form;
		// 统计总记录数的sql语句
		int resultSize = 0;
		// 初始化pageBean供分页所用
		PageBean pageBean = null;
		// 取得当前页list数据
		List yfAppointTaskList = null;
		resultSize = yfAppointTaskService.getAllCountYfAppointTask(yfAppointTask);
		pageBean = PageBean.getPageBean("yfAppointTaskTab", resultSize,request);
		yfAppointTaskList = yfAppointTaskService.searchYfAppointTask(yfAppointTask,pageBean);
		request.setAttribute("yfAppointTaskList", yfAppointTaskList);
		boolean isManager = yfProjectService.isProjectManager(CommonUtil.getCurrentUser(request).getId());
		request.setAttribute("isManager",isManager);
		log.debug("searchYfAppointTask" + "结束");

		return mapping.findForward(Constants.SUCCESS);
	}

        /**
	 * 到达新增页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward toAddYfAppointTask(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("toAddYfAppointTasks");
		YfAppointTaskForm yfAppointTaskForm = (YfAppointTaskForm) form;
		yfAppointTaskForm.setId(RandomKey.generateID_Long_19());
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 增加
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward addYfAppointTask(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("addYfAppointTasks");
		YfAppointTask yfAppointTask = this.getYfAppointTask(form);
		yfAppointTask.setAuditStatus(WorkFlowConstants.TASK_STATUS_1);
		 yfAppointTask.setOperateid(CommonUtil.getCurrentUser(request).getId());
		 yfAppointTask.setCreateDate(new Date());

		this.yfAppointTaskService.insertObject(yfAppointTask);

		return mapping.findForward(Constants.SUCCESS);
	}
        
	/**
	 * 删除
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward deleteYfAppointTask(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("deleteYfAppointTasks");
		YfAppointTaskForm yfAppointTaskForm = (YfAppointTaskForm) form;
		this.yfAppointTaskService.deleteYfAppointTasks(yfAppointTaskForm);
		String serverPath=CommonUtil.ATTACHE_FILE;
		if(StringUtils.isBlank(serverPath)){
			serverPath=request.getSession().getServletContext().getRealPath("/"); 
		}
		AttachmentForm attachmentForm = new AttachmentForm();
		attachmentForm.setFid(yfAppointTaskForm.getId().toString());
		attachmentForm.setType(com.manager.commons.Constants.FJ_TYPE_RWZP);
		List<Attachment> attachment = attachmentService.queryAttachment(attachmentForm);
		if(attachment!=null && attachment.size()>0){
			for (Attachment attachment2 : attachment) {
				File file = new File(serverPath + attachment2.getUrl());
		        file.delete();
				attachmentService.deleteObject(attachment2);
				
			}
		}
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 到达更新页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward toUpdateYfAppointTask(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("toUpdateYfAppointTasks");
		YfAppointTaskForm yfAppointTaskForm = (YfAppointTaskForm) form;
		yfAppointTask = this.yfAppointTaskService.searchYfAppointTask(yfAppointTaskForm,null).get(0);
		request.setAttribute(Constants.YFAPPOINTTASK, yfAppointTask);
		return mapping.findForward(Constants.SUCCESS);
	}

        
	/**
	 * 更新
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward updateYfAppointTask(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("updateYfAppointTasks");
		YfAppointTask yfAppointTask = this.getYfAppointTask(form);
	        yfAppointTask.setOperateid(CommonUtil.getCurrentUser(request).getId());
		 yfAppointTask.setCreateDate(new Date());
		this.yfAppointTaskService.updateObject(yfAppointTask);

		return mapping.findForward(Constants.SUCCESS);
	}
        
	/**
	 * 查看页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward viewYfAppointTask(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		log.debug("viewYfAppointTask");
		YfAppointTaskForm yfAppointTaskForm = (YfAppointTaskForm) form;
		yfAppointTask = this.yfAppointTaskService.searchYfAppointTask(yfAppointTaskForm,null).get(0);
		request.setAttribute(Constants.YFAPPOINTTASK, yfAppointTask);
		return mapping.findForward(Constants.SUCCESS);
	}

	// get--set
	/**
	 * @return the YfAppointTaskService
	 */
	public YfAppointTaskService getYfAppointTaskService() {
		return this.yfAppointTaskService;
	}

	/**
	 * @param YfAppointTaskService
	 *  the YfAppointTaskService to set
	 */
	public void setYfAppointTaskService(YfAppointTaskService yfAppointTaskService) {
		this.yfAppointTaskService = yfAppointTaskService;
	}

	/**
	 * 获取YfAppointTask实体
	 * @param ActionForm
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 * @return the YfAppointTask
	 */
	public YfAppointTask getYfAppointTask(ActionForm form) throws IllegalAccessException, InvocationTargetException {
		YfAppointTaskForm yfAppointTaskForm = (YfAppointTaskForm) form;
		yfAppointTask = new YfAppointTask();
		BeanUtils.copyProperties(yfAppointTask, yfAppointTaskForm);
		return yfAppointTask;
	}
    
        public void setYfAppointTask(YfAppointTask yfAppointTask) {
		this.yfAppointTask = yfAppointTask;
	}

		public AttachmentService getAttachmentService() {
			return attachmentService;
		}

		public void setAttachmentService(AttachmentService attachmentService) {
			this.attachmentService = attachmentService;
		}

		/**
		 * 提交审核
		 * 
		 * @param mapping
		 * @param form
		 * @param request
		 * @param response
		 * @return ActionForward
		 * @throws Exception
		 */
		public ActionForward submitWorkFlowYfAppointTask(ActionMapping mapping, ActionForm form,
				HttpServletRequest request, HttpServletResponse response)
				throws Exception {
			log.debug("submitWorkFlowYfAppointTask");
			String submitType = request.getParameter("submitType");
			//新增界面直接提交
			if(StringUtils.isNotBlank(submitType) && "add".equals(submitType)){
				yfAppointTask = this.getYfAppointTask(form);
				yfAppointTask.setOperateid(CommonUtil.getCurrentUser(request).getId());
				yfAppointTask.setCreateDate(new Date());
				this.yfAppointTaskService.insertObject(yfAppointTask);
			}
			if(StringUtils.isNotBlank(submitType) && "update".equals(submitType)){
				yfAppointTask = this.getYfAppointTask(form);
			}else{
				YfAppointTaskForm yfAppointTaskForm = (YfAppointTaskForm) form;
				yfAppointTask = yfAppointTaskService.searchYfAppointTask(yfAppointTaskForm, null).get(0);
			}
			Map<String,String> mapVar = new HashMap<String,String>();
			mapVar.put("bussId", yfAppointTask.getId().toString());
			mapVar.put("bussType", WorkFlowConstants.BUSS_TYPE_RWZP.toString());
			String taskName = yfAppointTask.getTaskContent();
			if(StringUtils.isNotBlank(taskName) && taskName.length()>10){
				taskName = taskName.substring(0,9);
			}
			mapVar.put("projectName",taskName); //任务标题
			mapVar.put("projectType", "");
			mapVar.put("projectOrg",yfAppointTask.getTaskBelong());//任务指派时放被指派人
			mapVar.put("projectNo", yfAppointTask.getOperateid()); //任务指派时放任务指派人
			mapVar.put("ownYear","0");  //任务指派 
			/**
			 * TO DO  流程发起人需要根据实际用户修改
			 */
			String processId = workFlowCommon.startFlow(WorkFlowConstants.PORCESS_DEFINITION_ZP,CommonUtil.getCurrentUser(request).getId(),mapVar);
		
			yfAppointTask.setProcessId(processId);
			yfAppointTask.setAuditStatus(WorkFlowConstants.TASK_STATUS_2);
			yfAppointTask.setOperateid(CommonUtil.getCurrentUser(request).getId());
			yfAppointTask.setCreateDate(new Date());
			this.yfAppointTaskService.updateObject(yfAppointTask);
			return mapping.findForward(Constants.SUCCESS);
		}

		/**
		 * 进入审核页面
		 * @param mapping
		 * @param form
		 * @param request
		 * @param response
		 * @return ActionForward
		 * @throws Exception
		 */
		public ActionForward toAuditYfAppointTask(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
			log.debug("toAuditYfAppointTask");
			YfAppointTaskForm yfAppointTaskForm = (YfAppointTaskForm) form;
			String bussId = request.getParameter("bussId");
			String processId = request.getParameter("processId");
			String workItemId = request.getParameter("workItemId");
			yfAppointTaskForm.setId(bussId);
			yfAppointTask = yfAppointTaskService.searchYfAppointTask(yfAppointTaskForm, null).get(0);
			yfAppointTask.setWorkItemId(workItemId);
			request.setAttribute(Constants.YFAPPOINTTASK, yfAppointTask);
			examineList(processId,yfExmaineService,request);
			return mapping.findForward(Constants.SUCCESS);
		}
		
		
		/**
		 * 处理审批 提交流程
		 * @param mapping
		 * @param form
		 * @param request
		 * @param response
		 * @return
		 * @throws Exception
		 */
		public ActionForward commandFlowYfAppointTask(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
			log.debug("commandFlowYfOutsourceContract");
			YfAppointTaskForm yfAppointTaskForm = (YfAppointTaskForm) form;
			yfAppointTask = yfAppointTaskService.searchYfAppointTask(yfAppointTaskForm, null).get(0);
			
			YfExmaine examine = addYfExmaine(yfAppointTaskForm,request);
			Map processVar = new HashMap();
			
			if(examine.getExamineFlag().equals("disagree")){  //如果选择不执行任务
				//首先更改记录状态为不执行
				yfAppointTask.setOpinion(yfAppointTaskForm.getOpinion());
				yfAppointTask.setAuditStatus(WorkFlowConstants.TASK_STATUS_4);
				yfAppointTaskService.updateObject(yfAppointTask);
				//提交流程
				processVar.put("bussId", yfAppointTask.getId().toString());
				processVar.put("bussType", WorkFlowConstants.BUSS_TYPE_RWZP.toString());
				String taskName = yfAppointTask.getTaskContent();
				if(StringUtils.isNotBlank(taskName) && taskName.length()>10){
					taskName = taskName.substring(0,9);
				}
				processVar.put("projectName",taskName); //任务标题
				processVar.put("projectType", "");
				processVar.put("projectOrg",yfAppointTask.getTaskBelong());//任务指派时放被指派人
				processVar.put("projectNo", yfAppointTask.getOperateid()); //任务指派时放任务指派人
				processVar.put("ownYear","0");  //任务指派 
				String workItemId = yfAppointTaskForm.getWorkItemId();
				List actorIdList = new ArrayList();
				actorIdList.add(yfAppointTask.getOperateid());
				workFlowCommon.commandFlowDynamicAssign(workItemId, processVar, actorIdList);
			}
			if(examine.getExamineFlag().equals("agree")){ //如果执行任务
				//首先更改记录状态为不执行
				yfAppointTask.setOpinion(yfAppointTaskForm.getOpinion());
				yfAppointTask.setAuditStatus(WorkFlowConstants.TASK_STATUS_3);
				yfAppointTaskService.updateObject(yfAppointTask);
			}
			if(examine.getExamineFlag().equals("tijiao")){ //任务执行完后提交
				//首先更改记录状态为执行完成
				yfAppointTask.setExeResult(yfAppointTaskForm.getExeResult());
				yfAppointTask.setAuditStatus(WorkFlowConstants.TASK_STATUS_5);
				yfAppointTaskService.updateObject(yfAppointTask);
				//提交流程
				processVar.put("bussId", yfAppointTask.getId().toString());
				processVar.put("bussType", WorkFlowConstants.BUSS_TYPE_RWZP.toString());
				String taskName = yfAppointTask.getTaskContent();
				if(StringUtils.isNotBlank(taskName) && taskName.length()>10){
					taskName = taskName.substring(0,9);
				}
				processVar.put("projectName",taskName); //任务标题
				processVar.put("projectType", "");
				processVar.put("projectOrg",yfAppointTask.getTaskBelong());//任务指派时放被指派人
				processVar.put("projectNo", yfAppointTask.getOperateid()); //任务指派时放任务指派人
				processVar.put("ownYear","0");  //任务指派 
				String workItemId = yfAppointTaskForm.getWorkItemId();
				List actorIdList = new ArrayList();
				actorIdList.add(yfAppointTask.getOperateid());
				workFlowCommon.commandFlowDynamicAssign(workItemId, processVar, actorIdList);
			}
			if(examine.getExamineFlag().equals("pinjia")){ //领导评价
				//首先更改记录状态为执行完成
				yfAppointTask.setRemark(yfAppointTaskForm.getRemark());
				yfAppointTask.setEvaluate(Long.valueOf(yfAppointTaskForm.getExamineValue()));
				yfAppointTask.setAuditStatus(WorkFlowConstants.TASK_STATUS_6);
				yfAppointTaskService.updateObject(yfAppointTask);
				//提交流程
				processVar.put("bussId", yfAppointTask.getId().toString());
				processVar.put("bussType", WorkFlowConstants.BUSS_TYPE_RWZP.toString());
				String taskName = yfAppointTask.getTaskContent();
				if(StringUtils.isNotBlank(taskName) && taskName.length()>10){
					taskName = taskName.substring(0,9);
				}
				processVar.put("projectName",taskName); //任务标题
				processVar.put("projectType", "");
				processVar.put("projectOrg",yfAppointTask.getTaskBelong());//任务指派时放被指派人
				processVar.put("projectNo", yfAppointTask.getOperateid()); //任务指派时放任务指派人
				processVar.put("ownYear","0");  //任务指派 
				String workItemId = yfAppointTaskForm.getWorkItemId();
				workFlowCommon.commandFlow(workItemId, processVar);
			}
			return mapping.findForward(Constants.SUCCESS);
		}
		
		/**
		 * Description: TODO 组合审批意见对象,添加
		 * 
		 * @param form
		 * @param request
		 * @return ActionForward
		 */
		public YfExmaine addYfExmaine(ActionForm form, HttpServletRequest request) {

			YfAppointTaskForm yfAppointTaskForm = (YfAppointTaskForm) form;
			// 审核标识
			String examineFlag = yfAppointTaskForm.getExamineFlag();
			// 审核意见
			String examineOption = yfAppointTaskForm.getExamineOption();
			String processId = yfAppointTaskForm.getProcessId();
			YfExmaine yfExmaine = new YfExmaine();
			yfExmaine.setId(RandomKey.generateID_Long_19());
			yfExmaine.setExamineFlag(examineFlag);
			yfExmaine.setExamineOption(examineOption);
			yfExmaine.setProcessId(processId);
			yfExmaine.setExamineDate(new Date());
			if(request.getSession().getAttribute("loginUser")!=null){
				Usertable curUser = (Usertable) request.getSession().getAttribute("loginUser");
				yfExmaine.setExaminePerson(curUser.getRname());
				yfExmaine.setExamineOrg("领导审批");
			}
			return yfExmaine;
		}
		
		 /*
		 * Description: TODO 查看显示意见
		 * 
		 * @param processInsId
		 * @param examine
		 * @param examineService
		 * @param examineLis
		 * @param request
		 *        void
		 */
		public void examineList(String processInsId, YfExmaineService yfExmaineService, HttpServletRequest request) throws Exception {
			YfExmaineForm yfExmaineForm = new YfExmaineForm();
			yfExmaineForm.setProcessId(processInsId);
			// 审批列表
			List<YfExmaine> examineList = yfExmaineService.searchYfExmaine(yfExmaineForm, null);
			// 防止页面出错
			int resultSize = 0;
			if(examineList != null && !examineList.isEmpty()) {
				resultSize = examineList.size();
			}
			DisplayTagPageBean.getPageBean("examine", resultSize, 0, request);
			request.setAttribute("examineList", examineList);

		}

		public WorkFlowCommon getWorkFlowCommon() {
			return workFlowCommon;
		}

		public void setWorkFlowCommon(WorkFlowCommon workFlowCommon) {
			this.workFlowCommon = workFlowCommon;
		}

		public YfExmaineService getYfExmaineService() {
			return yfExmaineService;
		}

		public void setYfExmaineService(YfExmaineService yfExmaineService) {
			this.yfExmaineService = yfExmaineService;
		}

		public void setYfProjectService(YfProjectService yfProjectService) {
			this.yfProjectService = yfProjectService;
		}
}
